from logging import exception
from time import sleep, strftime,localtime,time
from selenium.webdriver.common.by import By
from common.Utils import ForumDriver
from selenium.common.exceptions import NoSuchFrameException

scrpath = "C:\\ForumTest\\Screenshot"

class ForumHome:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver
        url = "http://127.0.0.1:58080/index.html"
        self.driver.get(url)

    def HomeBotton(self):
        # self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        # self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        # sleep(2)
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # sleep(1)
        for i in range(2,11):
            self.driver.find_element(By.CSS_SELECTOR, f"#topBoardList > li:nth-child({i}) > a").click()
            sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(1) > a").click()

    def HomePic(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img")
        self.driver.find_element(By.CSS_SELECTOR,"#index_nav_avatar")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col-auto > span")

    def HomePost(self):
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col > div.text-truncate > a > strong")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col > div.text-muted.mt-2 > div > div.col > ul > li:nth-child(2)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)")
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")

    def HomePostFunc(self):
        actual = self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div > h2").text
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(1) > a").click()

    def HomeSwitchMod(self):
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark").click()
        urls = self.driver.current_url
        file_name = strftime("%Y-%m-%d %H-%M-%S HomeSwitchMod", localtime())
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)
        assert urls == "http://127.0.0.1:58080/index.html?theme=dark"
        sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light").click()
        urls = self.driver.current_url
        file_name = strftime("%Y-%m-%d %H-%M-%S HomeSwitchMod", localtime()) + ".png"
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)
        assert urls == "http://127.0.0.1:58080/index.html?theme=light"

    def HomeUserbotton(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown").click()
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_profile")
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_settings")
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_logout")
        file_name = strftime("%Y-%m-%d %H-%M-%S HomeUserbotton", localtime())
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)

# F = ForumHome()
# F.ForumBotton()